package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IAttendanceDedItemDAO;
import org.jsola.hr.dto.AttendanceDedItemAddDTO;
import org.jsola.hr.dto.AttendanceDedItemUpdateDTO;
import org.jsola.hr.entity.AttendanceDedItemDO;
import org.jsola.hr.query.AttendanceDedItemQuery;
import org.jsola.hr.service.IAttendanceDedItemService;
import org.jsola.hr.vo.AttendanceDedItemListVO;
import org.jsola.hr.vo.AttendanceDedItemVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrAttendanceDedItemServiceImpl")
public class AttendanceDedItemServiceImpl implements IAttendanceDedItemService {

    @Autowired
    private IAttendanceDedItemDAO attendanceDedItemDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public AttendanceDedItemVO save(AttendanceDedItemAddDTO attendanceDedItemAddDTO, TokenUser tokenUser) {
        // 转DO
        AttendanceDedItemDO attendanceDedItemDO = attendanceDedItemAddDTO.to(AttendanceDedItemDO.class);
        // 保存
        attendanceDedItemDO = save(attendanceDedItemDO, tokenUser.getSiteId(), tokenUser.getUserId());
        HrLogKit.logForSave("新增勤扣款设置子项", "saveAttendanceDedItem", attendanceDedItemDO.getId(), tokenUser);
        return attendanceDedItemDO.to(AttendanceDedItemVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(AttendanceDedItemUpdateDTO attendanceDedItemUpdateDTO, TokenUser tokenUser) {
        //转DO
        AttendanceDedItemDO attendanceDedItemDO = attendanceDedItemUpdateDTO.to(AttendanceDedItemDO.class);
        HrLogKit.logForUpdate("修改勤扣款设置子项", "updateAttendanceDedItem", attendanceDedItemDO.getId(), tokenUser);
        //根据主键更新，只更新非null值
        return updateByIdSelective(attendanceDedItemDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... attendanceDedItemIds) {
        HrLogKit.logForDelete("批量删除勤扣款设置子项", "deleteAttendanceDedItemByIds", Arrays.toString(attendanceDedItemIds), tokenUser);
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) attendanceDedItemIds);
    }


    @Override
    public AttendanceDedItemVO selectById(String attendanceDedItemId, String siteId) {
        AttendanceDedItemDO attendanceDedItemDO = selectDOById(attendanceDedItemId, siteId);
        if (attendanceDedItemDO == null) {
            return null;
        }
        return attendanceDedItemDO.to(AttendanceDedItemVO.class);
    }

    @Override
    public List<AttendanceDedItemListVO> select(AttendanceDedItemQuery attendanceDedItemQuery, String siteId) {
        List<AttendanceDedItemDO> attendanceDedItemDOList = selectDO(attendanceDedItemQuery, siteId);
        if (CollectionUtils.isEmpty(attendanceDedItemDOList)) {
            return attendanceDedItemDOList == null ? null : new ArrayList<>();
        }
        return attendanceDedItemDOList.stream()
                .map(attendanceDedItemDO -> attendanceDedItemDO.to(AttendanceDedItemListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(AttendanceDedItemQuery attendanceDedItemQuery, String siteId) {
        Example example = buildExample(attendanceDedItemQuery, siteId);
        return attendanceDedItemDAO.selectCountByExample(example);
    }

    @Override
    public Page<AttendanceDedItemListVO> selectPage(AttendanceDedItemQuery attendanceDedItemQuery, String siteId) {
        Example example = buildExample(attendanceDedItemQuery, siteId);
        Page<AttendanceDedItemDO> page = attendanceDedItemDAO.selectPageByExample(example,
                attendanceDedItemQuery.getPageNo(),
                attendanceDedItemQuery.getPageSize());

        return page.to(AttendanceDedItemListVO.class);
    }

    @Override
    public AttendanceDedItemDO selectDOById(String attendanceDedItemId, String siteId) {
        return listById(attendanceDedItemId, siteId);
    }

    @Override
    public List<AttendanceDedItemDO> selectDO(AttendanceDedItemQuery attendanceDedItemQuery, String siteId) {
        Example example = buildExample(attendanceDedItemQuery, siteId);
        return attendanceDedItemDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param attendanceDedItemQuery 查询参数
     * @param siteId                 所属站点id
     * @return example
     */
    private Example buildExample(AttendanceDedItemQuery attendanceDedItemQuery, String siteId) {
        Example example = new Example(AttendanceDedItemDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (attendanceDedItemQuery.getTimeOperator() != null) {
            example.and().andEqualTo("timeOperator", attendanceDedItemQuery.getTimeOperator());
        }
        if (attendanceDedItemQuery.getTimeValue() != null) {
            example.and().andEqualTo("timeValue", attendanceDedItemQuery.getTimeValue());
        }
        if (attendanceDedItemQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", attendanceDedItemQuery.getCompanyId());
        }
        if (attendanceDedItemQuery.getParentId() != null) {
            example.and().andEqualTo("parentId", attendanceDedItemQuery.getParentId());
        }
        if (attendanceDedItemQuery.getLevel() != null) {
            example.and().andEqualTo("level", attendanceDedItemQuery.getLevel());
        }
        if (attendanceDedItemQuery.getGmtCreateStart() != null &&
                attendanceDedItemQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", attendanceDedItemQuery.getGmtCreateStart(),
                    attendanceDedItemQuery.getGmtCreateEnd());
        }
        if (attendanceDedItemQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + attendanceDedItemQuery.getCreateUserId() + "%");
        }
        if (attendanceDedItemQuery.getGmtModifiedStart() != null &&
                attendanceDedItemQuery.getGmtModifiedEnd() != null) {
            example.and().andBetween("gmtModified", attendanceDedItemQuery.getGmtModifiedStart(),
                    attendanceDedItemQuery.getGmtModifiedEnd());
        }
        if (attendanceDedItemQuery.getModifiedUserId() != null) {
            example.and().andLike("modifiedUserId", "%" + attendanceDedItemQuery.getModifiedUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example, attendanceDedItemQuery.getOrders());
        return example;
    }
}




